package o;

import android.util.Pair;
import com.google.android.exoplayer2.Format;
import com.google.android.exoplayer2.ParserException;
import com.google.android.exoplayer2.extractor.ts.TsPayloadReader.a;
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage;
import java.util.Arrays;
import java.util.Collections;

public final class afy implements agc {
    private static final byte[] ˎ = new byte[]{(byte) 73, (byte) 68, (byte) 51};
    private int ʻ;
    private int ʻॱ;
    private aef ʼ;
    private aef ʽ;
    private long ʽॱ;
    private aef ʾ;
    private final String ˊ;
    private int ˊॱ;
    private final anc ˋ;
    private int ˋॱ;
    private final boolean ˏ;
    private int ˏॱ;
    private boolean ͺ;
    private final ane ॱ;
    private boolean ॱˊ;
    private long ॱˋ;
    private boolean ॱˎ;
    private String ॱॱ;
    private int ॱᐝ;
    private int ᐝ;
    private long ᐝॱ;

    public afy(boolean z) {
        this(z, null);
    }

    public afy(boolean z, String str) {
        this.ॱ = new ane(new byte[7]);
        this.ˋ = new anc(Arrays.copyOf(ˎ, 10));
        ˎ();
        this.ˏॱ = -1;
        this.ˋॱ = -1;
        this.ॱˋ = -9223372036854775807L;
        this.ˏ = z;
        this.ˊ = str;
    }

    public static boolean ˎ(int i) {
        return (65526 & i) == 65520;
    }

    public void ˏ() {
        ˊ();
    }

    public void ˏ(ady o_ady, a aVar) {
        aVar.ˊ();
        this.ॱॱ = aVar.ˎ();
        this.ʽ = o_ady.ˊ(aVar.ˏ(), 1);
        if (this.ˏ) {
            aVar.ˊ();
            this.ʼ = o_ady.ˊ(aVar.ˏ(), 4);
            this.ʼ.ˎ(Format.ˏ(aVar.ˎ(), "application/id3", null, -1, null));
            return;
        }
        this.ʼ = new adx();
    }

    public void ˋ(long j, int i) {
        this.ᐝॱ = j;
    }

    public void ˊ(anc o_anc) throws ParserException {
        while (o_anc.ˊ() > 0) {
            switch (this.ᐝ) {
                case 0:
                    ˋ(o_anc);
                    break;
                case 1:
                    ˏ(o_anc);
                    break;
                case 2:
                    if (!ˏ(o_anc, this.ˋ.ˏ, 10)) {
                        break;
                    }
                    ॱॱ();
                    break;
                case 3:
                    if (!ˏ(o_anc, this.ॱ.ˎ, this.ͺ ? 7 : 5)) {
                        break;
                    }
                    ʽ();
                    break;
                case 4:
                    ˎ(o_anc);
                    break;
                default:
                    throw new IllegalStateException();
            }
        }
    }

    public void ˋ() {
    }

    public long ॱ() {
        return this.ॱˋ;
    }

    private void ˊ() {
        this.ॱˊ = false;
        ˎ();
    }

    private boolean ˏ(anc o_anc, byte[] bArr, int i) {
        int min = Math.min(o_anc.ˊ(), i - this.ʻ);
        o_anc.ˎ(bArr, this.ʻ, min);
        this.ʻ = min + this.ʻ;
        return this.ʻ == i;
    }

    private void ˎ() {
        this.ᐝ = 0;
        this.ʻ = 0;
        this.ˊॱ = 256;
    }

    private void ʻ() {
        this.ᐝ = 2;
        this.ʻ = ˎ.length;
        this.ॱᐝ = 0;
        this.ˋ.ˋ(0);
    }

    private void ˎ(aef o_aef, long j, int i, int i2) {
        this.ᐝ = 4;
        this.ʻ = i;
        this.ʾ = o_aef;
        this.ʽॱ = j;
        this.ॱᐝ = i2;
    }

    private void ʼ() {
        this.ᐝ = 3;
        this.ʻ = 0;
    }

    private void ᐝ() {
        this.ᐝ = 1;
        this.ʻ = 0;
    }

    private void ˋ(anc o_anc) {
        byte[] bArr = o_anc.ˏ;
        int ˋ = o_anc.ˋ();
        int ॱ = o_anc.ॱ();
        while (ˋ < ॱ) {
            int i = ˋ + 1;
            ˋ = bArr[ˋ] & 255;
            if (this.ˊॱ != WXMediaMessage.TITLE_LENGTH_LIMIT || !ˊ((byte) -1, (byte) ˋ) || (!this.ॱˊ && !ˋ(o_anc, i - 2))) {
                switch (ˋ | this.ˊॱ) {
                    case 329:
                        this.ˊॱ = 768;
                        ˋ = i;
                        break;
                    case 511:
                        this.ˊॱ = WXMediaMessage.TITLE_LENGTH_LIMIT;
                        ˋ = i;
                        break;
                    case 836:
                        this.ˊॱ = WXMediaMessage.DESCRIPTION_LENGTH_LIMIT;
                        ˋ = i;
                        break;
                    case 1075:
                        ʻ();
                        o_anc.ˋ(i);
                        return;
                    default:
                        if (this.ˊॱ == 256) {
                            ˋ = i;
                            break;
                        }
                        this.ˊॱ = 256;
                        ˋ = i - 1;
                        break;
                }
            }
            this.ʻॱ = (ˋ & 8) >> 3;
            this.ͺ = (ˋ & 1) == 0;
            if (this.ॱˊ) {
                ʼ();
            } else {
                ᐝ();
            }
            o_anc.ˋ(i);
            return;
        }
        o_anc.ˋ(ˋ);
    }

    private void ˏ(anc o_anc) {
        if (o_anc.ˊ() != 0) {
            this.ॱ.ˎ[0] = o_anc.ˏ[o_anc.ˋ()];
            this.ॱ.ˋ(2);
            int ॱ = this.ॱ.ॱ(4);
            if (this.ˋॱ == -1 || ॱ == this.ˋॱ) {
                if (!this.ॱˊ) {
                    this.ॱˊ = true;
                    this.ˏॱ = this.ʻॱ;
                    this.ˋॱ = ॱ;
                }
                ʼ();
                return;
            }
            ˊ();
        }
    }

    private boolean ˋ(anc o_anc, int i) {
        boolean z = true;
        o_anc.ˋ(i + 1);
        if (!ॱ(o_anc, this.ॱ.ˎ, 1)) {
            return false;
        }
        this.ॱ.ˋ(4);
        int ॱ = this.ॱ.ॱ(1);
        if (this.ˏॱ != -1 && ॱ != this.ˏॱ) {
            return false;
        }
        if (this.ˋॱ != -1) {
            if (!ॱ(o_anc, this.ॱ.ˎ, 1)) {
                return true;
            }
            this.ॱ.ˋ(2);
            if (this.ॱ.ॱ(4) != this.ˋॱ) {
                return false;
            }
            o_anc.ˋ(i + 2);
        }
        if (!ॱ(o_anc, this.ॱ.ˎ, 4)) {
            return true;
        }
        this.ॱ.ˋ(14);
        int ॱ2 = this.ॱ.ॱ(13);
        if (ॱ2 <= 6) {
            return false;
        }
        ॱ2 += i;
        if (ॱ2 + 1 >= o_anc.ॱ()) {
            return true;
        }
        if (!(ˊ(o_anc.ˏ[ॱ2], o_anc.ˏ[ॱ2 + 1]) && (this.ˏॱ == -1 || ((o_anc.ˏ[ॱ2 + 1] & 8) >> 3) == ॱ))) {
            z = false;
        }
        return z;
    }

    private boolean ˊ(byte b, byte b2) {
        return ˎ(((b & 255) << 8) | (b2 & 255));
    }

    private boolean ॱ(anc o_anc, byte[] bArr, int i) {
        if (o_anc.ˊ() < i) {
            return false;
        }
        o_anc.ˎ(bArr, 0, i);
        return true;
    }

    private void ॱॱ() {
        this.ʼ.ˎ(this.ˋ, 10);
        this.ˋ.ˋ(6);
        ˎ(this.ʼ, 0, 10, this.ˋ.ʿ() + 10);
    }

    private void ʽ() throws ParserException {
        int i = 2;
        this.ॱ.ˋ(0);
        if (this.ॱˎ) {
            this.ॱ.ˎ(10);
        } else {
            int ॱ = this.ॱ.ॱ(2) + 1;
            if (ॱ != 2) {
                amz.ˏ("AdtsReader", "Detected audio object type: " + ॱ + ", but assuming AAC LC.");
            } else {
                i = ॱ;
            }
            this.ॱ.ˎ(5);
            byte[] ॱ2 = amr.ॱ(i, this.ˋॱ, this.ॱ.ॱ(3));
            Pair ˋ = amr.ˋ(ॱ2);
            Format ˎ = Format.ˎ(this.ॱॱ, "audio/mp4a-latm", null, -1, -1, ((Integer) ˋ.second).intValue(), ((Integer) ˋ.first).intValue(), Collections.singletonList(ॱ2), null, 0, this.ˊ);
            this.ॱˋ = 1024000000 / ((long) ˎ.ʼॱ);
            this.ʽ.ˎ(ˎ);
            this.ॱˎ = true;
        }
        this.ॱ.ˎ(4);
        int ॱ3 = (this.ॱ.ॱ(13) - 2) - 5;
        if (this.ͺ) {
            ॱ3 -= 2;
        }
        ˎ(this.ʽ, this.ॱˋ, 0, ॱ3);
    }

    private void ˎ(anc o_anc) {
        int min = Math.min(o_anc.ˊ(), this.ॱᐝ - this.ʻ);
        this.ʾ.ˎ(o_anc, min);
        this.ʻ = min + this.ʻ;
        if (this.ʻ == this.ॱᐝ) {
            this.ʾ.ˋ(this.ᐝॱ, 1, this.ॱᐝ, 0, null);
            this.ᐝॱ += this.ʽॱ;
            ˎ();
        }
    }
}
